package com.example.chinawenddingexpo.service;

import com.example.chinawenddingexpo.model.WeddingService;
import com.example.chinawenddingexpo.repository.WeddingServiceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Service
public class WeddingServiceService {
    
    @Autowired
    private WeddingServiceRepository weddingServiceRepository;
    
    public List<WeddingService> getUserServices(Long userId) {
        return weddingServiceRepository.findByUserIdOrderByServiceDateDesc(userId);
    }
    
    public List<WeddingService> getUserServicesByStatus(Long userId, WeddingService.BookingStatus status) {
        return weddingServiceRepository.findByUserIdAndStatusOrderByServiceDateDesc(userId, status);
    }
    
    public List<WeddingService> getUpcomingServices(Long userId) {
        return weddingServiceRepository.findUpcomingServices(userId, LocalDate.now());
    }
    
    public List<WeddingService> getExhibitorServices(Long exhibitorId, LocalDate startDate, LocalDate endDate) {
        return weddingServiceRepository.findByExhibitorIdAndServiceDateBetweenOrderByServiceDateAsc(
                exhibitorId, startDate, endDate);
    }
    
    public WeddingService getServiceById(Long id) {
        Optional<WeddingService> service = weddingServiceRepository.findById(id);
        return service.orElse(null);
    }
    
    public WeddingService saveService(WeddingService weddingService) {
        return weddingServiceRepository.save(weddingService);
    }
    
    public WeddingService confirmBooking(Long serviceId) {
        WeddingService service = getServiceById(serviceId);
        if (service != null) {
            service.setStatus(WeddingService.BookingStatus.CONFIRMED);
            service.setConfirmTime(LocalDate.now().atStartOfDay());
            return weddingServiceRepository.save(service);
        }
        return null;
    }
    
    public WeddingService cancelBooking(Long serviceId) {
        WeddingService service = getServiceById(serviceId);
        if (service != null) {
            service.setStatus(WeddingService.BookingStatus.CANCELLED);
            return weddingServiceRepository.save(service);
        }
        return null;
    }
}